﻿@inject NavigationManager NavigationManager
@inject ServiceStackStateProvider AuthStateProvider
@inject HostState HostState

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData"
                            DefaultLayout="@GetPageLayout(routeData)">
            <NotAuthorized>
                <h1 class="mb-4 text-2xl font-semibold text-gray-900 dark:text-gray-100">
                    Sorry
                </h1>
                <p>You're not authorized to reach this page.</p>
                <p>
                    Please
                    <NavLink class="underline hover:text-green-600" @onclick=@login href="javascript:void">login</NavLink>
                    as a different user.
                </p>
            </NotAuthorized>

            <Authorizing>
                <p class="text-gray-400 absolute -mt-4 mr-4 right-0">Authenticating...</p>
                <RouteView RouteData="@routeData" />
            </Authorizing>

        </AuthorizeRouteView>
    </Found>
    <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <div class="min-h-full bg-white dark:bg-black px-4 py-16 sm:px-6 sm:py-24 md:grid md:place-items-center lg:px-8">
                    <div class="mx-auto max-w-max">
                        <main class="sm:flex">
                            <p class="text-4xl font-bold tracking-tight text-indigo-600 dark:text-indigo-400 sm:text-5xl">404</p>
                            <div class="sm:ml-6">
                                <div class="sm:border-l sm:border-gray-200 dark:sm:border-gray-800 sm:pl-6">
                                    <h1 class="text-4xl font-bold tracking-tight text-gray-900 dark:text-gray-100 sm:text-5xl">Page not found</h1>
                                    <p class="mt-2 text-base text-gray-500 dark:text-gray-400">Please check the URL in the address bar and try again.</p>
                                </div>
                                <div class="mt-10 flex space-x-3 sm:border-l sm:border-transparent sm:pl-6">
                                    <PrimaryButton href="/">Go back home</PrimaryButton>
                                    <SecondaryButton href="#">Contact support</SecondaryButton>
                                </div>
                            </div>
                        </main>
                    </div>
                </div>
            </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>

@code {
    Type GetPageLayout(RouteData route) => route.PageType?.FirstAttribute<LayoutAttribute>()?.LayoutType ?? typeof(MainLayout);

    [Parameter] public InitialHostState? InitialState { get; set; }

    protected override Task OnInitializedAsync()
    {
        HostState.Load(InitialState);
        return base.OnInitializedAsync();
    }

    async Task login()
    {
        await AuthStateProvider.LogoutIfAuthenticatedAsync(redirectTo: NavigationManager.GetLoginUrl());
    }
}